/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.plan.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.plan.dao.InStoreVoucherDAO;
import com.javaeye.plan.dto.InStoreVoucher;
import com.javaeye.plan.web.VoucherCondition;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.util.ListUtil;
import com.javaeye.common.web.PageInfo;

public class InStoreVoucherService implements IInStoreVoucherService {
	
	private static Log log = LogFactory.getLog(InStoreVoucherService.class);
	
	protected InStoreVoucherDAO dao;
	
	public void setDao(InStoreVoucherDAO dao) {
		this.dao = dao;
	}

	@SuppressWarnings("unchecked")
	public List getVoucherList(VoucherCondition condition, PageInfo pageInfo) {
		List result = dao.getPageData(condition, pageInfo);
		if (result == null || result.size() == 0) {
			log.warn("数据库中未找到指定的入库单信息");
		}
		return result;
	}

	public InStoreVoucher getInStoreVoucher(String voucherId) {
		InStoreVoucher voucher = dao.getInStoreVoucher(voucherId);
		if (voucher == null) {
			log.warn("数据库中未找到指定的入库单信息，单号：" + voucherId);
			return new InStoreVoucher();
		}
		return voucher;
	}
	
	public void removeInStoreVoucher(String voucherId) {
		dao.removeInStoreVoucher(voucherId);
	}
	
	public void saveInStoreVoucher(InStoreVoucher voucher) {
		if (null == voucher.getId() || "".equals(voucher.getId())) {
			String id = ListUtil.PRODUCT_INSTORE_ORDER_ID_PRE + DateUtils.dateToString("yyyyMM");
			int maxId = dao.getMaxInStoreVoucherId(id) + 1;
			id = id + String.format("%04d", maxId);
			log.debug("生成的成品入库单编号：" + id);
			//生成成品入库单ID
			voucher.setId(id);
		}
		voucher.setStatus(InStoreVoucher.VOUCHES_STATES_CREATED);
		voucher.setUpdateDate(DateUtils.now());
		dao.saveInStoreVoucher(voucher);
	}
}
